package xml.tests;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.ParserFactory;

import xml.ConverterToUidXml;

public class TestConverter {

	ConverterToUidXml converter;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		converter = new ConverterToUidXml();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCD1() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD1", "tmp");
		try {
			converter.convert("src/testFiles/UC1-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC1-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD2() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD2", "tmp");
		try {
			converter.convert("src/testFiles/UC2-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC2-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD3() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD3", "tmp");
		try {
			converter.convert("src/testFiles/UC3-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC3-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD4() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD4", "tmp");
		try {
			converter.convert("src/testFiles/UC4-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC4-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD5() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD5", "tmp");
		try {
			converter.convert("src/testFiles/UC5-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC5-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD6() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD6", "tmp");
		try {
			converter.convert("src/testFiles/UC6-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC6-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD7() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD7", "tmp");
		try {
			converter.convert("src/testFiles/UC7-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC7-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD8() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD8", "tmp");
		try {
			converter.convert("src/testFiles/UC8-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC8-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD9() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD9", "tmp");
		try {
			converter.convert("src/testFiles/UC9-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC9-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD10() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD10", "tmp");
		try {
			converter.convert("src/testFiles/UC10-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC10-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD11() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD11", "tmp");
		try {
			converter.convert("src/testFiles/UC11-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC11-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD12() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD12", "tmp");
		try {
			converter.convert("src/testFiles/UC12-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC12-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	@Test
	public void testCD13() throws Exception {
		File output = File.createTempFile("output_TestConverter_testCD13", "tmp");
		try {
			converter.convert("src/testFiles/UC13-CD.xuid",output.getAbsolutePath());
			assertFileEquals("src/testFiles/UC13-CD.xml",output.getAbsolutePath());
		} finally {
			if (!output.delete())
				output.deleteOnExit();
		}
	}

	private static void assertFileEquals(String expected, String actual) throws IOException {
		File f1 = new File(expected);
		File f2 = new File(actual);
		FileInputStream fis1 = new FileInputStream(f1);
		FileInputStream fis2 = new FileInputStream(f2);
		if (f1.length() == f2.length()) {
			int n = 0;
			byte[] b1;
			byte[] b2;
			while ((n = fis1.available()) > 0) {
				if (n > 80)
					n = 80;
				b1 = new byte[n];
				b2 = new byte[n];
				int res1 = fis1.read(b1);
				int res2 = fis2.read(b2);
				if (!Arrays.equals(b1, b2)) {
					Assert.failNotEquals("output mismatch",new String(b1),new String(b2));
				}
			}
		} else
			Assert.failNotEquals("length mismatch",f1.length(),f2.length());
	}
}
